Skip to content

fix(combobox): open menu when initially-empty async items load - #10994

Open
bellussi1 wants to merge 2 commits into
adobe:mainfrom
bellussi1:fix/combobox-initially-empty-async
Open

bellussi1 wants to merge 2 commits into
adobe:mainfrom
bellussi1:fix/combobox-initially-empty-async

Conversation

@bellussi1

Copy link
Copy Markdown
Contributor

Closes #2333

Summary

Problem and intent: When an async ComboBox starts with an empty collection (e.g. an empty search string returns []) and does not set allowsEmptyCollection, typing the first character fails to open the menu once async results arrive. In useComboBoxState, closedDueToEmpty (introduced in #9823) was only set when closing an already-open menu (triggerState.isOpen && filteredCollection.size === 0). On the first keystroke from a closed empty state (!triggerState.isOpen), open(null, 'input') is skipped while results are pending and lastValue updates to inputValue; on the later render when items arrive, inputValue === lastValue and closedDueToEmpty.current is false, so the menu stays closed.

Implementation:

  • In packages/react-stately/src/combobox/useComboBoxState.ts, when an input change occurs while focused and closed with menuTrigger !== 'manual', if filteredCollection.size === 0 && !allowsEmptyCollection, set closedDueToEmpty.current = true so incoming async items open the menu cleanly on arrival.
  • Cleared closedDueToEmpty.current when opening due to filteredCollection.size > 0 || allowsEmptyCollection, when blurring (setFocused(false), regardless of shouldCloseOnBlur/isReadOnly), when manually toggling the menu closed (toggleMenu), and when closing upon single-selection change (displayValue !== lastValueRef.current). (Explicit dismissals via revert() / commitValue() already cleared it.)
  • Added regression tests in packages/react-stately/test/combobox/useComboBoxState.test.js and packages/react-aria-components/test/ComboBox.test.js.

📝 Test Instructions

  1. Regression tests (failing before useComboBoxState.ts change -> passing after):
    • yarn test packages/react-stately/test/combobox/useComboBoxState.test.js -t "initially-empty" --runInBand
      • Before fix: failed (should open the menu when initially-empty controlled items load after typing received isOpen === false).
      • After fix: passed (2 passed).
    • yarn test packages/react-aria-components/test/ComboBox.test.js -t "initially-empty" --runInBand
      • Before fix: failed (both ComboBox items and ListBox items useAsyncList tests failed with listbox staying null).
      • After fix: passed (3 passed; onOpenChange called once with (true, 'input') on load and once with (false, undefined) on backspace to empty).
    • Negative guard tests added alongside them verify blurring or pressing Escape while async results are pending clears the pending open and keeps the menu closed.
  2. Full ComboBox unit, SSR, formatting, and lint runs on Windows (Node v24.21.0):
    • yarn format -> exited 0 (oxfmt across 6,570 files)
    • yarn test packages/react-stately/test/combobox/useComboBoxState.test.js --runInBand -> 20 passed (20 total)
    • yarn test packages/react-aria/test/combobox/useComboBox.test.js --runInBand -> 6 passed (6 total)
    • yarn test packages/react-aria-components/test/ComboBox.test.js --runInBand -> 53 passed (53 total)
    • yarn test packages/@adobe/react-spectrum/test/combobox/ComboBox.test.js --runInBand -> 216 passed (216 total)
    • yarn test packages/@react-spectrum/s2/test/Combobox.test.tsx --runInBand -> 7 passed (7 total)
    • yarn test:ssr packages/react-aria-components/test/ComboBox.ssr.test.js --testTimeout=180000 -> 2 passed (2 total; first cold run timed out at 120s starting the local SSR server, warm rerun passed in 30.1s)
    • yarn lint -> exited 0 (format:check, tsc --noEmit, oxlint packages with 0 warnings/0 errors across 2,847 files, scripts/lint-packages.js, yarn constraints)

Covered: useComboBoxState hook tests, RAC ComboBox (ComboBox items and ListBox items with useAsyncList), Spectrum ComboBox, S2 Combobox, ComboBox SSR hydration, blur/Escape cancellation, and monorepo lint/typecheck. Not manually verified: Storybook visual testing, Playwright browser suite (yarn test:browser), or screen readers.

Release impact

  • Only changes docs, tests, CI, or development tools. Nothing shipped to users changes.
  • Fixes a bug or improves existing code without changing how apps use it.
  • Adds a feature or API while keeping existing usage working.
  • May break existing usage or require changes in apps that use this code.

✅ PR Checklist

  • I followed the contributing guide.
  • I tested this change locally and included the results in Test Instructions, or explained why testing does not apply.
  • I understand and can explain every change in this PR. If I used AI assistance, I followed the AI contribution guidance.

AI assistance was used during root-cause analysis and test authoring; all changes follow AGENTS.md and CONTRIBUTING.md, were reviewed line by line, and were verified locally with the commands above.

🧢 Your Project

Independent open-source contribution.

@snowystinger snowystinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, would you mind adding a storybook story to demonstrate the behaviour. The tests are good for regressions, but with how complex combobox is it'd be good to verify it manually in a real browser in addition to the jsdom tests

@bellussi1

Copy link
Copy Markdown
Contributor Author

Added Storybook stories demonstrating the initially-empty useAsyncList behavior in both React Aria Components/ComboBox (ComboBoxAsyncInitiallyEmptyExample) and ComboBox (ServerSideFilteringInitiallyEmpty).

@bellussi1 bellussi1 closed this Nov 18, 2026
@bellussi1 bellussi1 reopened this Nov 18, 2026
@bellussi1 bellussi1 closed this Nov 18, 2026
@bellussi1 bellussi1 reopened this Nov 18, 2026
}
};

export const ComboBoxAsyncInitiallyEmptyExample: ComboBoxStory = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I see a difference when i type "F", but the behaviour before and after is the same for "f". It doesn't open until I delete and re-type the "f", then they both open. I'm surprised that "f" works on the second time, but not the first. Any idea what's going on there? is it related to this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the second try part is related. On main, typing "f" the first time still loads [Foo] into useAsyncList even though the menu stays closed. When you backspace, useAsyncList keeps [Foo] for a render while inputValue changes to "", which briefly opens the menu. Then load({filterText: ''}) resolves [], so it auto-closes and sets closedDueToEmpty.current = true. That's why typing "f" again opens on both branches.

I'm not sure why "f" vs "F" would behave differently on the first try though. Both the story (filterText.toLowerCase()) and useComboBoxState are case-insensitive, and lowercase works in the tests. Did "f" blur the input or hit a shortcut, or does it happen consistently on a fresh reload?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice it blur or hit a shortcut, it happened every time i tried it. Were you able to reproduce it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just spun up Storybook locally on this branch (Chromium) and tested both ComboBoxAsyncInitiallyEmptyExample and ServerSideFilteringInitiallyEmpty, but I couldn't reproduce the difference. Both "f" and "F" opened the menu on the first keystroke for me.

Which browser/OS and story are you seeing that in? Also, if you inspect the wrapper when "f" fails to open, does .react-aria-ComboBox still have data-focused="true"? That might help narrow down whether something in the manager or browser setup is dropping focus or intercepting the key.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must've had something in my storybook cache, i cleared out everything and it's no longer different between the two. Thanks for also trying it out

@snowystinger snowystinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ok, thanks for the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

combobox open state breaks for initially-empty async searches

2 participants